/*
 * TransTester.java
 *
 * Created on March 5, 2007, 6:01 PM
 *
 */

package edu.byu.isys413.jjenkins.data;

import java.sql.Date;

 /**
 * The transTester class test the functionality of the transaction. It reads
 * transactions from the database as well as creates new transactions.
 * It updates the transaction if possible and returns items if possible (the 
 * item must exist on an old transaction). It also creates sample data to 
 * make these transactions (such as product, employee, store, customer, etc.
 * 
 * @author      Jeff Jenkins
 * @version     1.0
 * 
 */
public class TransTester {
    
    /** Creates a new instance of TransTester */
    public TransTester() {
    }
    public static void main(String[] args){
        
        try{
            
//****************these are the transactions from the default database. Tests read methods********
            
            TransactionBO transA1 = TransactionDAO.getInstance().read("trans1");
            transA1.display();
            TransactionBO transA2 =TransactionDAO.getInstance().read("trans2");
            transA2.display();
            TransactionBO transA3 =TransactionDAO.getInstance().read("trans3");
            transA3.display();
            TransactionBO transA4 =TransactionDAO.getInstance().read("trans4");
            transA4.display();
            
            
            
            
//***************** This is creates new information in the database*********************
            
            
            //Create Employees
            EmployeeBO emp1 = EmployeeDAO.getInstance().create();
            emp1.setHireDate(new Date(System.currentTimeMillis()));
            emp1.setName("Johnny Jenkins");
            emp1.setPhone("80789234");
            emp1.setPosition(null);
            EmployeeDAO.getInstance().save(emp1);
            
            EmployeeBO emp2 = EmployeeDAO.getInstance().create();
            emp2.setHireDate(new Date(System.currentTimeMillis()));
            emp2.setName("Harry");
            emp2.setPhone("77777777");
            emp2.setPosition(null);
            EmployeeDAO.getInstance().save(emp2);
            System.out.println("Create Employees");
            
            // Creates Stores
            StoreBO store1 = StoreDAO.getInstance().create();
            store1.setAddress("1849 N. 200 W. Provo, UT 84604");
            store1.setLocation("Provo");
            store1.setManager(emp1);
            store1.setPhone("1234567");
            StoreDAO.getInstance().save(store1);
            
            StoreBO store2 = StoreDAO.getInstance().create();
            store2.setAddress("Kitty Corner 123");
            store2.setLocation("Hoosville");
            store2.setManager(emp2);
            store2.setPhone("44444444444");
            StoreDAO.getInstance().save(store2);
            System.out.println("Created Stores");
            
            
            //Creates Customer
            CustomerBO cust1 = CustomerDAO.getInstance().create();
            cust1.setFirstName("Homer");
            cust1.setLastName("Simpson");
            cust1.setEmail("dogs@ddd.com");
            cust1.setAddress("742 Evergreen Terrace");
            cust1.setCity("Springfield");
            cust1.setState("Unknown");
            cust1.setZip("84235");
            cust1.setPhone("801-224-0234");
            CustomerDAO.getInstance().save(cust1);
            
            //Create a second customer
            CustomerBO cust2 = CustomerDAO.getInstance().create();
            cust2.setFirstName("Marge");
            cust2.setLastName("Simpson");
            cust2.setEmail("pigs@dude.com");
            cust2.setAddress("742 Evergree Terrace");
            cust2.setCity("Unknown");
            cust2.setZip("84602");
            cust2.setPhone("801-224-0233");
            CustomerDAO.getInstance().save(cust2);;
            System.out.println("Created Customers");
            
            //Create Member
            MemberBO mem1 = MembershipDAO.getInstance().create();
            cust1.setMember(mem1);
            mem1.setCustomer(cust1);
            mem1.setCreditCard("123412345");
            mem1.setStartDate(new Date(System.currentTimeMillis()));
            mem1.setExpireDate(new Date(System.currentTimeMillis()));
            MembershipDAO.getInstance().save(mem1);
            System.out.println("Created Member");
            
            //Create a area of interests
            InterestBO int1 = InterestDAO.getInstance().create();
            int1.setDescription("MP3 Players");
            cust1.getMember().setAreaOfInterest(int1.getId());
            InterestDAO.getInstance().save(int1);
            System.out.println("saved new interest");
            CustomerDAO.getInstance().save(cust1);
            
            InterestBO int2 = InterestDAO.getInstance().create();
            int2.setDescription("Video Players");
            cust1.getMember().setAreaOfInterest(int2.getId());
            InterestDAO.getInstance().save(int2);
            CustomerDAO.getInstance().save(cust1);
            System.out.println(cust2.isObjectAlreadyInDB());
            CustomerDAO.getInstance().save(cust2);
            System.out.println("Created Membership areas of Interest");
            
            
            
            // Create RevenueSource
            
            RevenueSourceBO revenue1 = (RevenueSourceFactory.getInstance().getDAO("example")).create();
            revenue1.setPrice(5);
            revenue1.setQuantity(2);
            revenue1.setDescription("Lens Cleaner");
            (RevenueSourceFactory.getInstance().getDAO("example")).save(revenue1);
            
            RevenueSourceBO revenue2 = (RevenueSourceFactory.getInstance().getDAO("example")).create();
            revenue2.setPrice(670);
            revenue2.setQuantity(2);
            revenue2.setDescription("Video Camera x123");
            (RevenueSourceFactory.getInstance().getDAO("example")).save(revenue2);
            System.out.println("Created Revenue Source");
            
            
            
            // Create Coupon
            ConceptualCouponBO coupon1 = ConceptualCouponDAO.getInstance().create();
            coupon1.setAmount(.5);
            ConceptualCouponDAO.getInstance().save(coupon1);
            
            ConceptualCouponBO coupon2 = ConceptualCouponDAO.getInstance().create();
            coupon2.setAmount(.5);
            ConceptualCouponDAO.getInstance().save(coupon2);
            System.out.println("Created Coupons");
            
            
//*************************New Sale********************************************/
            System.out.println("\n\n\n New Sales Transaction");
            
            // Sign In (Creates CurrentOpts
            CurrentOptsBO currentOpt1 = CurrentOptsDAO.getInstance().create();
            currentOpt1.SignIn(emp1.getId(), store1.getId());
            
            
            // create sales transaction. The controller would grab this information from the GUI to start a transaction
            TransactionBO trans1 = TransactionDAO.getInstance().create();
            trans1.setType("Sale");
            trans1.setCustomer(cust1);
            trans1.setDate(new Date(System.currentTimeMillis()));
            if(trans1.getType() == "Return"){
                trans1.setOriginalTrans(trans1);
            }
            
            
            // Create add transaction lines for trans1
            TransactionLineBO line1 = TransactionLineDAO.getInstance().create();
            line1.setTransaction(trans1);
            line1.setRevenueSource(revenue1);
            line1.setCoupon(coupon1);
            trans1.addTransactionLine(line1);
            
            // Create add transaction lines for trans1
            TransactionLineBO line2 = TransactionLineDAO.getInstance().create();
            line2.setTransaction(trans1);
            line2.setRevenueSource(revenue2);
            trans1.addTransactionLine(line2);
            
            // Create add transaction lines for trans1
            TransactionLineBO line3 = TransactionLineDAO.getInstance().create();
            line3.setTransaction(trans1);
            line3.setRevenueSource((RevenueSourceFactory.getInstance().getDAO("example")).read("rev1"));
            line3.setCoupon(ConceptualCouponDAO.getInstance().read("cou1"));
            trans1.addTransactionLine(line3);
            
            // Create add transaction lines for trans1
            TransactionLineBO line4 = TransactionLineDAO.getInstance().create();
            line4.setTransaction(trans1);
            line4.setRevenueSource((RevenueSourceFactory.getInstance().getDAO("example")).read("rev2"));
            trans1.addTransactionLine(line4);
            
            
            // finish Order
            trans1.endTransaction();
            
            // create payment
            PaymentBO payment = PaymentDAO.getInstance().create();
            payment.setType("credit");
            payment.setAmount(1000);
            
            
            // Post transaction
            UpdateController.getInstance().post(trans1,payment,currentOpt1);
            
            
            // Sign Out
            currentOpt1.SignOut();
            
//*************************Sales Return********************************************/
            
            System.out.println("\n\n\n New Return Transaction");
            
            // Sign In (Creates CurrentOpts
            CurrentOptsBO currentOpt2 = CurrentOptsDAO.getInstance().create();
            currentOpt2.SignIn(emp2.getId(), store2.getId());
            
            
            // create sales transaction. The controller would grab this information from the GUI to start a transaction
            TransactionBO trans2 = TransactionDAO.getInstance().create();
            trans2.setType("Return");
            trans2.setCustomer(cust1);
            trans2.setDate(new Date(System.currentTimeMillis()));
            if(trans2.getType().equals("Return")){
                trans2.setOriginalTrans(trans1);
            }
            
            
            // Returns an item: Checks that the item exists on the original transaction
            // and gets coupon information.
            
            TransactionLineBO line9 = TransactionLineDAO.getInstance().create();
            line9.setTransaction(trans2);
            line9.setRevenueSource(revenue1);
            trans2.addTransactionLine(line9);
            
            
            // finish Order
            trans2.endTransaction();
            
            // create payment
            PaymentBO payment1 = PaymentDAO.getInstance().create();
            
            
            // Post transaction
            UpdateController.getInstance().post(trans2,payment1,currentOpt2);
            
            
            // Sign Out
            currentOpt1.SignOut();
            
            
            /************************Try to return an item that was not on the original transaction*/
            try{
                System.out.println("\n\n\n************************************************************************************\nTrying to return an item that is not on the orginal transaction\n");
                
                
                
                // create sales transaction. The controller would grab this information from the GUI to start a transaction
                TransactionBO trans3 = TransactionDAO.getInstance().create();
                trans3.setType("Return");
                trans3.setCustomer(cust1);
                trans3.setDate(new Date(System.currentTimeMillis()));
                if(trans3.getType().equals("Return")){
                    trans3.setOriginalTrans(transA1);
                }
                
                
                // Returns an item: Checks that the item exists on the original transaction
                // and gets coupon information.
                
                TransactionLineBO line10 = TransactionLineDAO.getInstance().create();
                line10.setTransaction(trans3);
                line10.setRevenueSource(revenue1);
                trans3.addTransactionLine(line10);
                
                
                // finish Order
                trans3.endTransaction();
                
                // create payment
                PaymentBO payment3 = PaymentDAO.getInstance().create();
                
                
                // Post transaction
                UpdateController.getInstance().post(trans3,payment3,currentOpt2);
                
                
                // Sign Out
                currentOpt1.SignOut();
                
                
            }catch(DataException ex){
                System.out.println(ex);
            }
            
            
            /***************************Changing status of pending transaction to hold*/
            System.out.println("\n\n\n\nChanging status of pending transaction to hold");
            TransactionBO trans4 = TransactionDAO.getInstance().read("trans4");
            trans4.setStatus("hold");
            
                // I have two post methods one that takes a payment and finalizes the transaction and the otehr one (this one used) which just sets the current opts and saves it to the database
            UpdateController.getInstance().post(trans4, currentOpt1);
            TransactionDAO.getInstance().save(trans4);
            trans4.display();
            
            /***************************Changing status of pending transaction to void*/
            System.out.println("\n\n\n\nChanging status of pending transaction to resume or pending");
            trans4.setStatus("pending");
            TransactionDAO.getInstance().save(trans4);
            trans4.display();
            
            /***************************Changing status of pending transaction to void*/
            System.out.println("\n\n\n\nChanging status of pending transaction to void after read from database");
            trans4.setStatus("void");
            TransactionDAO.getInstance().save(trans4);
            trans4.display();
            
            /************************Try edit a transaction that is finished*/
            try{
                System.out.println("\n\n\n************************************************************************************\nTrying to edit a transaction that is finished\n");
                TransactionDAO.getInstance().read(trans1.getId());
                trans1.setType("Rental");
                TransactionDAO.getInstance().save(trans1);
            }catch(DataException ex){
                System.out.println(ex);
            }
            
        }catch(DataException ex){
            System.out.println(ex);
            ex.printStackTrace();
        }
        
    }
    
    
    
    
    
    
    
}
